package com.kaduoxee.ebooks.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.kaduoxee.ebooks.entity.Bookshelf;
import com.kaduoxee.ebooks.entity.Fiction;
import com.kaduoxee.ebooks.entity.User;
import com.kaduoxee.ebooks.service.BookshelfService;
import com.kaduoxee.ebooks.service.impl.BookshelfServiceImpl;
import com.kaduoxee.ebooks.service.impl.FictionServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.xml.ws.soap.Addressing;
import java.util.ArrayList;
import java.util.List;

@Controller
public class BookshelfController {

    @Resource
    private BookshelfServiceImpl bookshelfService;

    @Resource
    private FictionServiceImpl fictionService;

    /**
     *书架页面
     */
    @RequestMapping("/shelf")
    public String shelf(HttpSession session, Model model){
        //userid(shelf表中查询)-->bookshelfs(shelf表)-->fictionIds(fiction表中查询)-->fictionList
        User user = (User) session.getAttribute("user");

        QueryWrapper<Bookshelf> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", user.getId());
        List<Bookshelf> bookshelfList = bookshelfService.list(wrapper);
        model.addAttribute("shelfList", bookshelfList);

        List<Fiction> fictionList = new ArrayList<>();
        for (Bookshelf bookshelf : bookshelfList) {
            fictionList.add(fictionService.getById(bookshelf.getFictionId()));
        }
        model.addAttribute("list", fictionList);
        return "/shelf/shelf";
    }

    /**
     * 判断user的书架上是否有某本书
     * 若有-->false
     * 若无，添加该小说
     * 即新增一个shelf-->true
     */
    @PostMapping("shelf/{fictionId}")
    @ResponseBody
    public boolean saveShelf(@PathVariable Integer fictionId,HttpSession session) {
        //获取userId
        User user = (User) session.getAttribute("user");
        Integer userId = user.getId();

        boolean flag = bookshelfService.hasFiction(userId, fictionId);

        if (flag) return false;

        //存入mysql
        Bookshelf bookshelf = new Bookshelf();
        bookshelf.setFictionId(fictionId);
        bookshelf.setUserId(userId);

        return bookshelfService.save(bookshelf);
    }

    /**
     * 移出书架后重定向"/shelf"
     */
    @DeleteMapping(value = "/shelf/{fictionId}")
    public String delete(@PathVariable Integer fictionId,HttpSession session) {
        User user = (User) session.getAttribute("user");
        System.out.println("================"+fictionId+"=====================");

        QueryWrapper<Bookshelf> wrapper = new QueryWrapper<>();
        wrapper.eq("fiction_id",fictionId).eq("user_id",user.getId());

        bookshelfService.remove(wrapper);
        return "redirect:/shelf";
    }

    /**
     * 继续阅读
     */
    @GetMapping("/shelf/read/{fictionId}/{sort}")
    public String readOn(@PathVariable Integer fictionId,@PathVariable Integer sort,HttpSession session) {
        String tailPath = fictionId+"/"+sort;

        User user = (User) session.getAttribute("user");
        UpdateWrapper<Bookshelf> wrapper = new UpdateWrapper<>();
        wrapper.set("sort", sort);
        wrapper.eq("user_id",user.getId()).eq("fiction_id", fictionId);
        bookshelfService.update(wrapper);

        return "forward:/chapter/read/"+tailPath;
    }


}
